<template>
  <page-header-wrapper>
    <a-card>
      <div class="table-page-search-wrapper">
        <a-form layout="inline" class="table-head-ground">
          <div class="table-layer">
            <a-form-item label="" class="table-head-layout" style="max-width:350px;min-width:300px">
            <a-range-picker
              @change="onChange"
              :show-time="{ format: 'HH:mm:ss' }"
              format="YYYY-MM-DD HH:mm:ss"
              :disabled-date="disabledDate"
              :default-value="[moment().subtract(7, 'days'),moment().endOf('day')]"
            >
                <a-icon slot="suffixIcon" type="sync" />
              </a-range-picker>
            </a-form-item>
            <jeepay-text-up :placeholder="'转账/商户/渠道订单号'" :msg="searchData.unionOrderId" v-model="searchData.unionOrderId" />
            <!--            <jeepay-text-up :placeholder="'转账订单号'" :msg="searchData.transferId" v-model="searchData.transferId" />-->
            <!--            <jeepay-text-up :placeholder="'商户订单号'" :msg="searchData.mchOrderNo" v-model="searchData.mchOrderNo" />-->
            <!--            <jeepay-text-up :placeholder="'渠道支付订单号'" :msg="searchData.channelOrderNo" v-model="searchData.channelOrderNo" />-->
            <jeepay-text-up :placeholder="'商户号'" :msg="searchData.mchNo" v-model="searchData.mchNo" />
            <jeepay-text-up :placeholder="'应用AppId'" :msg="searchData.appId" v-model="searchData.appId"/>
            <a-form-item label="" class="table-head-layout">
              <a-select v-model="searchData.state" placeholder="转账状态" default-value="">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="0">订单生成</a-select-option>
                <a-select-option value="1">转账中</a-select-option>
                <a-select-option value="2">转账成功</a-select-option>
                <a-select-option value="3">转账失败</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item v-if="$access('ENT_PAY_ORDER_SEARCH_PAY_WAY')" label="" class="table-head-layout">
              <a-select v-model="searchData.wayCode" placeholder="支付方式" default-value="">
                <a-select-option value="">全部</a-select-option>
                <a-select-option :key="item.wayCode" v-for="item in payWayList" :value="item.wayCode">
                  {{ item.wayName }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <span class="table-page-search-submitButtons">
              <a-button type="primary" icon="search" @click="queryFunc" :loading="btnLoading">搜索</a-button>
              <a-button style="margin-left: 8px" icon="reload" @click="() => this.searchData = {}">重置</a-button>
            </span>
          </div>
           <p v-show="dateObj">
                  <span class="data-margin">{{`提交订单数:[${dateObj?dateObj.totalCount:''}]`}}</span>
                  <span class="data-margin"> {{`订单总金额:[${dateObj?dateObj.totalAmount:''}]`}}</span>
                  <span class="data-margin"> {{`手续费总额:[${dateObj?dateObj.feeTotalAmount:''}]`}}</span>
                 <!-- <span>单笔手续费总额:[] &nbsp;&nbsp;</span> -->
                  <span class="data-margin"> {{`已付总金额:[${dateObj?dateObj.payTotalAmount:''}]`}}</span>
                  <span class="data-margin"> {{`已付订单数:[${dateObj?dateObj.payOrderCount:''}]笔`}}</span>
                  <span class="data-margin"> {{`未付总金额:[${dateObj?dateObj.unpayTotalAmount:''}]`}}</span>
                  <span class="data-margin"> {{`未付订单数: [${dateObj?dateObj.unpayOrderCount:''}]笔`}}</span>
                  <span class="data-margin"> {{`商户出款:[${dateObj?dateObj.payTotalAmountTo:''}]`}}</span>
            </p>
        </a-form>
      </div>
      <!-- 列表渲染 -->
      <JeepayTable
        @btnLoadClose="btnLoading=false"
        ref="infoTable"
        :initData="true"
        :reqTableDataFunc="reqTableDataFunc"
        :tableColumns="tableColumns"
        :searchData="searchData"
        rowKey="transferId"
        :tableRowCrossColor="true"
      >
        <template slot="transferAmountSlot" slot-scope="{record}">{{ record/100 }}</template> <!-- 自定义插槽 -->
        <template slot="mchFeeAmountSlot" slot-scope="{record}">{{ record/100 }}</template> <!-- 自定义插槽 -->
        <template slot="amountToSlot" slot-scope="{record}">{{ record/100 }}</template> <!-- 自定义插槽 -->
        <template slot="stateSlot" slot-scope="{record}">
          <a-tag
            :key="record.state"
            :color="record.state === 0?'blue':record.state === 1?'orange':record.state === 2?'green':'volcano'"
          >
            {{ record.state === 0?'订单生成':record.state === 1?'转账中':record.state === 2?'转账成功':record.state === 3?'转账失败':record.state === 4?'任务关闭':'未知' }}
          </a-tag>
        </template>
        <template slot="mchNoSlot" slot-scope="{record}">
          {{ record.mchNo }}
          <br/>
          <a-tooltip>
            <template slot="title">
              {{ record.appId }}
            </template>
            {{ record.appId }}
          </a-tooltip>
        </template>
        <template slot="orderSlot" slot-scope="{record}">
          <div class="order-list">
            <p><span style="color:#729ED5;background:#e7f5f7">转账</span>{{ record.transferId }}</p>
            <p style="margin-bottom: 0;">
              <span style="color:#56cf56;background:#d8eadf">商户</span>
              <a-tooltip placement="bottom" style="font-weight: normal;" v-if="record.mchOrderNo.length > record.transferId.length">
                <template slot="title">
                  <span>{{ record.mchOrderNo }}</span>
                </template>
                {{ changeStr2ellipsis(record.mchOrderNo, record.transferId.length) }}
              </a-tooltip>
              <span style="font-weight: normal;" v-else>{{ record.mchOrderNo }}</span>
            </p>
            <p v-if="record.channelOrderNo" style="margin-bottom: 0;margin-top: 10px">
              <span style="color:#fff;background:#E09C4D">渠道</span>
              <a-tooltip placement="bottom" style="font-weight: normal;" v-if="record.channelOrderNo.length > record.transferId.length">
                <template slot="title">
                  <span>{{ record.channelOrderNo }}</span>
                </template>
                {{ changeStr2ellipsis(record.channelOrderNo, record.transferId.length) }}
              </a-tooltip>
              <span style="font-weight: normal;" v-else>{{ record.channelOrderNo }}</span>
            </p>
          </div>
        </template>
        <template slot="opSlot" slot-scope="{record}">  <!-- 操作列插槽 -->
          <JeepayTableColumns>
            <a-button type="link" v-if="$access('ENT_TRANSFER_ORDER_VIEW')" @click="detailFunc(record.transferId)">详情</a-button>
            <a-dropdown>
              <a-menu slot="overlay">
                <a-menu-item key="1" style="color: red;" @click="transferOrders(record.transferId, 'f')">
                  测试失败 -> 通知
                </a-menu-item>
                <a-menu-item key="2" style="color: green;" @click="transferOrders(record.transferId, 's')">
                  测试成功 -> 通知
                </a-menu-item>
                <a-menu-item key="3" style="color: red;" @click="transferOrders(record.transferId, 'tf')">
                  转账失败-冻结转代付账户 -> 通知
                </a-menu-item>
                <a-menu-item key="4" style="color: green;" @click="transferOrders(record.transferId, 'ts')">
                  转账成功-冻结转已代付 -> 通知
                </a-menu-item>
                <a-menu-item key="5" style="color: red;" @click="transferOrders(record.transferId, 'tr')">
                  转账失败-状态反转 -> 通知
                </a-menu-item>
              </a-menu>
              <a-button type="link">
                更多 <a-icon type="down" />
              </a-button>
            </a-dropdown>
          </JeepayTableColumns>
        </template>
      </JeepayTable>
    </a-card>
    <!-- 订单详情 页面组件  -->
    <TransferOrderDetail ref="transferOrderDetail" />

  </page-header-wrapper>
</template>
<script>
  import JeepayTable from '@/components/JeepayTable/JeepayTable'
  import JeepayTextUp from '@/components/JeepayTextUp/JeepayTextUp' // 文字上移组件
  import JeepayTableColumns from '@/components/JeepayTable/JeepayTableColumns'
  import TransferOrderDetail from './TransferOrderDetail'
  import { API_URL_PAYWAYS_LIST, API_URL_TRANSFER_ORDER_LIST, req, transferOrders, API_URL_TRANSFER_ORDER_LIST_COUNT } from '@/api/manage'
  import moment from 'moment'

  // eslint-disable-next-line no-unused-vars
  const tableColumns = [
    { title: '商户名称', dataIndex: 'mchName', width: 100 },
    { title: '转账金额', dataIndex: 'amount', scopedSlots: { customRender: 'transferAmountSlot' }, width: 80 },
    { title: '转账手续费', dataIndex: 'mchFeeAmount', scopedSlots: { customRender: 'mchFeeAmountSlot' }, width: 80 },
    { title: '商户实扣', dataIndex: 'amountTo', scopedSlots: { customRender: 'amountToSlot' }, width: 80 },
    { key: 'transferId', title: '订单号', ellipsis: true, width: 220, scopedSlots: { customRender: 'orderSlot' } },
    { key: 'mchNo', title: '商户号', ellipsis: true, width: 220, scopedSlots: { customRender: 'mchNoSlot' } },
    { dataIndex: 'wayCode', title: '支付方式', width: 100 },
    { title: '收款账号', dataIndex: 'accountNo', width: 200 },
    { title: '收款人姓名', dataIndex: 'accountName', width: 200 },
    { title: '状态', scopedSlots: { customRender: 'stateSlot' }, width: 100 },
    { key: 'wayName', title: '支付方式', dataIndex: 'wayName', width: 80 },
    { title: '创建日期', dataIndex: 'createdAt', width: 200 },
    { title: '操作', width: '120px', fixed: 'right', align: 'center', scopedSlots: { customRender: 'opSlot' } }
  ]

  export default {
    name: 'IsvListPage',
    components: { JeepayTable, JeepayTableColumns, JeepayTextUp, TransferOrderDetail },
    data () {
      return {
        btnLoading: false,
        tableColumns: tableColumns,
        searchData: {
            createdStart: moment().subtract(7, 'days').format('YYYY-MM-DD HH:mm:ss'),
            createdEnd: moment().format('YYYY-MM-DD HH:mm:ss')
         },
        createdStart: '', // 选择开始时间
        createdEnd: '', // 选择结束时间
        payWayList: [],
        dateObj: {}
      }
    },
    mounted () {
      if (this.$access('ENT_PAY_ORDER_SEARCH_PAY_WAY')) {
        this.initPayWay()
      }
    },
    methods: {
        getDate (params) {
          req.list(API_URL_TRANSFER_ORDER_LIST_COUNT, params).then(res => { // 支付方式下拉列表
            this.dateObj = res
          })
        },
      queryFunc () {
        this.btnLoading = true
        this.$refs.infoTable.refTable(true)
      },
      // 请求table接口数据
      reqTableDataFunc (params) {
        this.getDate(params)
        return req.list(API_URL_TRANSFER_ORDER_LIST, params)
      },
      searchFunc: function () { // 点击【查询】按钮点击事件
        this.$refs.infoTable.refTable(true)
      },
      detailFunc: function (recordId) {
        this.$refs.transferOrderDetail.show(recordId)
      },
      moment,
      onChange (date, dateString) {
        this.searchData.createdStart = dateString[0] // 开始时间
        this.searchData.createdEnd = dateString[1] // 结束时间
      },
      disabledDate (current) { // 今日之后日期不可选
        return current && current > moment().endOf('day')
      },
      initPayWay: function () {
        const that = this
        req.list(API_URL_PAYWAYS_LIST, { 'pageSize': -1 }).then(res => { // 支付方式下拉列表
          that.payWayList = res.records
        })
      },
      changeStr2ellipsis (orderNo, baseLength) {
        const halfLengh = parseInt(baseLength / 2)
        return orderNo.substring(0, halfLengh - 1) + '...' + orderNo.substring(orderNo.length - halfLengh, orderNo.length)
      },
      transferOrders (id, sign) {
        transferOrders(id, sign).then(res => {
          this.queryFunc()
          this.$message.success('')
        }).catch(err => {
          console.log(err, 'err-transferOrders')
        })
      }
    }
  }
</script>
<style lang="less" scoped>
.order-list {
  -webkit-text-size-adjust:none;
  font-size: 12px;
  display: flex;
  flex-direction: column;

  p {
    white-space:nowrap;
    span {
      display: inline-block;
      font-weight: 800;
      height: 16px;
      line-height: 16px;
      width: 35px;
      border-radius: 5px;
      text-align: center;
      margin-right: 2px;
    }
  }
}
</style>
